package com.ruoyi.organization.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.CommunityScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.BizUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.organization.domain.BizFamily;
import com.ruoyi.organization.domain.BizFamilyScoreLog;
import com.ruoyi.organization.domain.BizPeople;
import com.ruoyi.organization.domain.vo.ChangeFamilyScore;
import com.ruoyi.organization.mapper.BizFamilyMapper;
import com.ruoyi.organization.service.IBizFamilyScoreLogService;
import com.ruoyi.organization.service.IBizFamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 家庭积分Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@Service
public class BizFamilyServiceImpl implements IBizFamilyService 
{
    @Autowired
    private BizFamilyMapper bizFamilyMapper;
    @Autowired
    private IBizFamilyScoreLogService bizFamilyScoreLogService;

    @Autowired
    private BizPeopleServiceImpl bizPeopleService;

    /**
     * 查询家庭积分
     * 
     * @param familyId 家庭积分主键
     * @return 家庭积分
     */
    @Override
    public BizFamily selectBizFamilyByFamilyId(Long familyId)
    {
        return bizFamilyMapper.selectBizFamilyByFamilyId(familyId);
    }


    /**
     * 查询家庭积分列表
     *
     * @param bizFamily 家庭积分
     * @return 家庭积分
     */
    @Override
    @CommunityScope(alias = "a")
    public List<BizFamily> selectBizFamilyListPage(BizFamily bizFamily)
    {
        if (Objects.nonNull(bizFamily.getTmp()) && Objects.isNull(bizFamily.getFamilyName())) {
            BizPeople bizPeople = bizPeopleService.selectBizPeopleByPeopleId(bizFamily.getTmp());
            if (Objects.nonNull(bizPeople)) {
                bizFamily.setFamilyHeadCard(bizPeople.getHeadCard());
            }
        }
        if (Objects.nonNull(bizFamily.getMemberName()) ) {
            BizPeople bizPeople = bizPeopleService.selectBizPeopleByPeopleName(bizFamily.getMemberName());
            if (Objects.nonNull(bizPeople)) {
                bizFamily.setFamilyHeadCard(bizPeople.getHeadCard());
            }
        }
        PageUtils.startPage();
        List<BizFamily> bizFamilies = bizFamilyMapper.selectBizFamilyList(bizFamily);
        for (BizFamily family : bizFamilies) {
            family.setFamilyHeadCard(BizUtils.maskIdCard(family.getFamilyHeadCard()));
            family.setFamilyMobile(BizUtils.maskPhoneNumber(family.getFamilyMobile()));
        }
        return bizFamilies;
    }

    private void fullFamilyHeadName(List<BizFamily> bizFamilies) {
        if (CollUtil.isEmpty(bizFamilies)){
            return;
        }
        Set<String> familyHeadCards = new HashSet<>();
        for (BizFamily bizFamily : bizFamilies) {
            if (StrUtil.isNotBlank(bizFamily.getFamilyHeadCard())) {
                familyHeadCards.add(bizFamily.getFamilyHeadCard());
            }
        }
        if (CollUtil.isEmpty(familyHeadCards)){
            return;
        }
        BizPeople bizPeople = new BizPeople();
        bizPeople.setPeopleCardList(ListUtil.toList(familyHeadCards));
        Map<String, BizPeople> bizPeopleMap = bizPeopleService.selectBizPeopleMap(bizPeople);
        for (BizFamily family : bizFamilies) {
            family.setFamilyName(MapUtil.getStr(bizPeopleMap, family.getFamilyHeadCard(), ""));
        }
    }

    /**
     * 查询家庭积分列表
     * 
     * @param bizFamily 家庭积分
     * @return 家庭积分
     */
    @Override
    @CommunityScope(alias = "a")
    public List<BizFamily> selectBizFamilyList(BizFamily bizFamily,String type)
    {
        if (Objects.nonNull(bizFamily.getTmp())) {
            BizPeople bizPeople = bizPeopleService.selectBizPeopleByPeopleId(bizFamily.getTmp());
            if (Objects.nonNull(bizPeople)) {
                bizFamily.setFamilyHeadCard(bizPeople.getHeadCard());
            }
        }
        List<BizFamily> bizFamilies = bizFamilyMapper.selectBizFamilyList(bizFamily);
        for (BizFamily family : bizFamilies) {
            family.setFamilyHeadCard(BizUtils.maskIdCard(family.getFamilyHeadCard()));
            if (StrUtil.isBlank(type)) {
                family.setFamilyMobile(BizUtils.maskPhoneNumber(family.getFamilyMobile()));
            }
        }
        return bizFamilies;
    }

    /**
     * 新增家庭积分
     * 
     * @param bizFamily 家庭积分
     * @return 结果
     */
    @Override
    public int insertBizFamily(BizFamily bizFamily)
    {
        bizFamily.setCreateTime(DateUtils.getNowDate());
        Boolean b = this.notExist(bizFamily);
        if (b) {
            return bizFamilyMapper.insertBizFamily(bizFamily);
        }
        else {
            return 0;
        }
    }

    /**
     * 检查家庭积分是否存在
     * @param bizFamily
     */
    private Boolean notExist(BizFamily bizFamily) {
        BizFamily family = this.bizFamilyMapper.findByHeadCard(bizFamily.getFamilyHeadCard());
        if (Objects.isNull(family)) {
            return true;
        }
        else {
            return false;
        }
    }
    /**
     * 修改家庭积分
     * 
     * @param bizFamily 家庭积分
     * @return 结果
     */
    @Override
    public int updateBizFamily(BizFamily bizFamily)
    {
        bizFamily.setUpdateTime(DateUtils.getNowDate());
        return bizFamilyMapper.updateBizFamily(bizFamily);
    }

    /**
     * 批量删除家庭积分
     * 
     * @param familyIds 需要删除的家庭积分主键
     * @return 结果
     */
    @Override
    public int deleteBizFamilyByFamilyIds(Long[] familyIds)
    {
        return bizFamilyMapper.deleteBizFamilyByFamilyIds(familyIds);
    }

    /**
     * 删除家庭积分信息
     * 
     * @param familyId 家庭积分主键
     * @return 结果
     */
    @Override
    public int deleteBizFamilyByFamilyId(Long familyId)
    {
        return bizFamilyMapper.deleteBizFamilyByFamilyId(familyId);
    }

    /**
     * 家庭积分调整
     *
     * @param changeFamilyScore
     * @return
     */
    @Transactional
    @Override
    public int changeScore(ChangeFamilyScore changeFamilyScore) {

        LoginUser loginUser = SecurityUtils.getLoginUser();

        BizFamily bizFamily = this.selectBizFamilyByFamilyId(changeFamilyScore.getFamilyId());
        bizFamily.setUpdateBy(loginUser.getUser().getUserName());
        bizFamily.setUpdateTime(DateUtils.getNowDate());

        BizFamilyScoreLog bizFamilyScoreLog = new BizFamilyScoreLog();
        String orderSerialNo = "JF" + IdUtil.getSnowflakeNextIdStr();
        bizFamilyScoreLog.setOrderSerialNo(orderSerialNo);
        bizFamilyScoreLog.setOrderNo(orderSerialNo);
        bizFamilyScoreLog.setPeopleId(loginUser.getUser().getUserId());
        bizFamilyScoreLog.setFamilyId(changeFamilyScore.getFamilyId());
        if (changeFamilyScore.getOrderType().equals(7L)){//+
            bizFamilyScoreLog.setOrderScoreType(1L);
            bizFamily.setFamilyScore(changeFamilyScore.getChangeScore()+bizFamily.getFamilyScore());
        }
        else if (changeFamilyScore.getOrderType().equals(8L)){//-
            bizFamilyScoreLog.setOrderScoreType(2L);
            bizFamily.setFamilyScore(bizFamily.getFamilyScore()-changeFamilyScore.getChangeScore());
        }
        else {

        }
        bizFamilyScoreLog.setOrderType(changeFamilyScore.getOrderType());
        bizFamilyScoreLog.setChangeScore(changeFamilyScore.getChangeScore());
        bizFamilyScoreLog.setTotalScore(bizFamily.getFamilyScore());
        bizFamilyScoreLog.setRemark(changeFamilyScore.getRemark());
        bizFamilyScoreLog.setCreateBy(loginUser.getUser().getUserName());
        bizFamilyScoreLog.setCreateTime(DateUtils.getNowDate());
        this.updateBizFamily(bizFamily);
        return bizFamilyScoreLogService.insertBizFamilyScoreLog(bizFamilyScoreLog);
    }

    /**
     * 更换家庭账号
     *
     * @param newHeadCard
     * @param oldHeadCard
     */
    @Override
    public int updateBizFamilyByHeadCard(String newHeadCard, String oldHeadCard,String familyName,String familyNameMobile) {
        return bizFamilyMapper.updateBizFamilyByHeadCard(newHeadCard,oldHeadCard, familyName,familyNameMobile);
    }
}
